<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xlink">
  <xsl:output method="xml" indent="yes" />
  <xsl:key name="competences-by-type" match="Competence" use="Type" />
  
  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm">
          <fo:region-body region-name="xsl-region-body" margin="2cm" /> <!-- Corps de page -->
          <fo:region-before region-name="xsl-region-before" extent="1cm" margin="4cm"  /> <!-- En-tete de page -->
          <fo:region-after region-name="xsl-region-after" extent="1cm" /> <!-- Pied de page -->
        </fo:simple-page-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="A4" initial-page-number="1">
        <fo:static-content flow-name="xsl-region-before">
        

          <fo:block-container padding-before="2mm">
      

          <fo:block text-align="center" font-size="18pt" background-color="#CEEEEE" color="#1531ec">LISTE des COMPÉTENCES</fo:block>
          </fo:block-container>
        </fo:static-content>
        
        <fo:static-content flow-name="xsl-region-after">
          <fo:block font-size="8pt" text-align="right" margin-right="1cm">Page <fo:page-number/> sur <fo:page-number-citation ref-id="last-page" /></fo:block>      
        </fo:static-content>

        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates />
          <fo:block id="last-page" />
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
  
  <xsl:template match="Competences">
    <xsl:for-each select="Competence[count(. | key('competences-by-type', Type)[1]) = 1]">
      <xsl:sort select="Type" />
      <fo:block-container>
        <fo:block font-size="15pt" background-color="#EEEEEE"><xsl:value-of select="Type" /></fo:block>
        
        <xsl:for-each select="key('competences-by-type', Type)">

          <xsl:sort select="." />
          <fo:block font-size="10pt" margin-left="5mm"><xsl:value-of select="Nom" /></fo:block>
        
        </xsl:for-each>
        <fo:block break-after="page"/>
        <fo:block padding-after="3mm"/>
      </fo:block-container>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
